<template>
  <div class="groupManage-page app-container">
    <el-form ref="dateForm" :inline="true" label-width="80px" :model="dateForm">
      <el-form-item label="分类名称" prop="classfyName">
        <el-input v-model="dateForm.classfyName" size="small" clearable @keyup.enter.native="searchList" placeholder="请输入分类名称" style="width: 220px;"></el-input>
      </el-form-item>
      <el-form-item label="所属组合" prop="compositionName">
        <el-input v-model="dateForm.compositionName" size="small" clearable @keyup.enter.native="searchList" placeholder="请输入所属组合" style="width: 220px;"></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 20px">
        <el-button class="bg-green" size="small" @click="searchList">查询</el-button>
        <!-- <el-button v-if="!isOpen" type="danger" @click="addOrUpdateHandle()">添加</el-button> -->
        <el-button size="small" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border fit
      size="mini"
      @row-click="submitHandle"
      :header-cell-style="{background:'#dcedff',color:'#32639d'}"
      style="width: 100%;"
    >
      <el-table-column prop="classifyId" label="编号" align="center" width="60" fixed></el-table-column>
      <el-table-column prop="classfyName" label="分类名称" min-width="160" fixed show-overflow-tooltip>
        <template slot-scope="{row}">
          <span v-if="row.hasNetWorth" style="margin-left: 10px;cursor: pointer;color: #1890ff;" @click.stop="groupDeltailHandle(row.classifyId)">{{ row.classfyName }}</span>
          <span v-else style="margin-left: 10px;cursor: pointer;color: #666;" @click.stop="$message('该分类净值不存在，无法查看详情')">{{ row.classfyName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="" label="对比" align="center" fixed>
        <template slot-scope="{row, $index}">
          <span v-if="row.hasNetWorth" class="pointer" :class="{'fontcolor': !row.addContrast}" @click="addContrasthandle(row, $index)">
            <span v-show="!row.addContrast" class="iconfont icon-jia" />
            <span v-show="row.addContrast" class="iconfont icon-check contrastGray" />
            <span :class="{'contrastGray': row.addContrast}" style="padding-left: 2px;">{{!row.addContrast ? '对比' : '已加'}}</span>
          </span>
          <span v-else class="contrastGray" @click.stop="$message('该分类净值不存在，不能对比')">
            <span class="iconfont icon-jia" />
            <span style="padding-left: 2px;">对比</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="compositionName" label="所属组合" min-width="130" fixed show-overflow-tooltip></el-table-column>
      <el-table-column prop="initMoney" label="投资金额(万元)" width="130" align="center"></el-table-column>
      <el-table-column prop="sinceCreatedIncomRate" label="收益率" min-width="100" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.sinceCreatedIncomRate" 
          :class="row.sinceCreatedIncomRate>=0?'positiveFc':'negativeFc'">{{ row.sinceCreatedIncomRate }}%</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="sinceOneYearIncomRate" label="近1年收益率" min-width="120" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.sinceOneYearIncomRate" 
          :class="row.sinceOneYearIncomRate>=0?'positiveFc':'negativeFc'">{{ row.sinceOneYearIncomRate }}%</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="sinceCreatedVolatility" label="波动率" min-width="100" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.sinceCreatedVolatility">{{ row.sinceCreatedVolatility }}%</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="sinceOneYearVolatility" label="近1年波动率" min-width="120" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.sinceOneYearVolatility">{{ row.sinceOneYearVolatility }}%</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
       <el-table-column prop="sinceCreatedSharpeRate" label="夏普率" min-width="100" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.sinceCreatedSharpeRate">{{ row.sinceCreatedSharpeRate }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="sinceOneYearSharpeRate" label="近1年夏普率" min-width="120" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.sinceOneYearSharpeRate">{{ row.sinceOneYearSharpeRate }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="sinceCreatedMaxPullBack" label="最大回撤" min-width="100" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.sinceCreatedMaxPullBack">{{ row.sinceCreatedMaxPullBack }}%</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="sinceOneYearMaxPullBack" label="近1年最大回撤" min-width="120" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.sinceOneYearMaxPullBack">{{ row.sinceOneYearMaxPullBack }}%</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="createdName" label="创建人" min-width="100" show-overflow-tooltip align="center"></el-table-column>
      <el-table-column prop="createdTime" label="创建日期" width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createdTime | parseTime }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="!isOpen" label="操作" align="center" width="130">
        <template slot-scope="{row}">
          <el-button class="filter-item" size="mini" type="primary" icon="el-icon-coin" @click="goDownStrategyHandle(row)">
            下属策略
          </el-button>
          <!-- <el-button class="filter-item" size="mini" style="margin-left: 10px;" type="primary" icon="el-icon-sort" @click="relationHandle(row)">
            关联策略
          </el-button>
          <el-button class="filter-item" size="mini" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="addOrUpdateHandle(row)">
            编辑
          </el-button>
          <el-button class="filter-item" size="mini" style="margin-left: 10px;" type="primary" icon="el-icon-delete" @click="deleteHandle(row)">
            删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageCurrent" :limit.sync="listQuery.limit" @pagination="getList" />

    <addOrUpdateOpen v-if="addOrUpdateView" ref="addOrUpdateOpen" :userId="userId" @closeOpen="getList"></addOrUpdateOpen>
    <globalContrastOpen ref="globalContrastOpen" v-show="contrastList.length" :list="contrastList"></globalContrastOpen>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Pagination from '@/components/Pagination'
import { findStrategyClassifyByPage, deleteStrategyClassifyById } from '@/api/groupManage'
import globalContrastOpen from '@/components/investComn/globalContrastOpen.vue'
import addOrUpdateOpen from './addOrUpdateOpen.vue'
import { rmoney } from '@/utils'

export default {
  name: 'GroupManage',
  components: { addOrUpdateOpen, Pagination, globalContrastOpen },
  props: {
    isOpen: {
      type: Boolean,
      defaultL: false
    }
  },
  data() {
    return {
      tableKey: 1,
      addContrasting: false, // 是否有勾选全局对比

      strategyVisible: false,
      addOrUpdateView: false,
      listLoading: false,
      dateForm: {
        classfyName: '',
        compositionName: ''
      },
      total: 0,
      list: [],
      listQuery: {
        pageCurrent: 1,
        limit: 20
      },
      rmoney: Function
    }
  },
  computed: {
    ...mapGetters(['userId', 'contrastList'])
  },
  watch: {
    contrastList(newList, oldList) {
      if(newList.length == oldList.length) { // todo 此处newList==oldList则是数组减少，未知原因
        this.syncContrastListHandle()
      }
      if(!newList.length) {
        this.list.forEach((row) => {
          row.addContrast = false
        })
        this.tableKey++
      }
    }
  },
  created() {
    this.dateForm.compositionId = this.$route.query.compositionId
    this.dateForm.compositionName = this.$route.query.compositionName
    this.rmoney = rmoney
    this.getList()
  },
  activated() {
    if(this.contrastList.length) {
      this.$refs.globalContrastOpen.showOpen = true
    }
  },
  methods: {
    // 同步对比框数据和列表数据
    syncContrastListHandle() {
      let tag = 0
      const list = this.contrastList.filter(item => item.contrastObj.type==2)
      this.list.forEach((row, index) => {
        row.addContrast = false
        const i = list.findIndex(item => item.contrastObj.id==row.classifyId)
        if(i!=-1) {
          tag = 1
          this.list[index].addContrast = true
        }
      })
      if(tag) this.tableKey++
    },
    addContrasthandle(row, i) {
      this.$refs.globalContrastOpen.showOpen = true
      row.addContrast = !row.addContrast
      this.$set(this.list, i, row)  // 更新表格数据
      if(row.addContrast) {
        row.contrastObj = {
          type: 2,
          id: row.classifyId,
          name: row.classfyName,
        }
        this.$store.commit('contrast/addContrastList', row)
      }else {
        const i = this.contrastList.findIndex(item => item.contrastObj.id==row.classifyId&&item.contrastObj.type==2)
        this.$store.commit('contrast/removeContrastList', i)
      }
    },
    relationHandle(row) {
      this.$router.push({
        path: '/strategyComb/groupManage/relationStrategy',
        query: {
          classifyId: row.classifyId
        }
      })
    },
    goDownStrategyHandle(row) {
      this.$router.push({
        path: '/strategyComb/strategyList',
        query: {
          classifyId: row.classifyId,
          classifyName: row.classfyName,
          compositionId: row.compositionId,
          compositionName: row.compositionName,
        }
      })
    },
    groupDeltailHandle(id) {
      this.$router.push({
        path: `/strategyComb/groupManage/netWorthAnalysis/${id}`
      })
    },
    submitHandle(row) {
      this.$emit('selctCombination', { classifyId: row.classifyId, classfyName: row.classfyName })
    },
    addOrUpdateHandle(row) {
      this.addOrUpdateView = true
      this.$nextTick(() => {
        this.$refs.addOrUpdateOpen.init(row)
      })
    },
    deleteHandle(row) {
      this.$confirm(`确认删除分类名称【${row.classfyName}】此条数据？`, '提示', {
        type: 'warning'
      }).then(_ => {
        deleteStrategyClassifyById({
          classifyId: row.classifyId
        }).then(res => {
          if(res.code == 0) {
            this.$notify({
              message: res.msg, 
              type: 'success',
              duration: 2000
            })
            this.getList()
          }
        })
      }).catch(_ => {})
    },
    resetForm() {
      this.$refs.dateForm.resetFields()
      this.dateForm = {
        classfyName: '',
        compositionName: ''
      }
      this.$router.push('/strategyComb/groupManage')
      this.getList()
    },
    searchList() {
      this.listQuery.pageCurrent = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      findStrategyClassifyByPage({
        ...this.dateForm,
        ...this.listQuery,
        userId: this.userId
      }).then(res => {
        this.listLoading = false
        this.list = res.body.list
        this.total = res.body.total

        if(this.contrastList.length) this.syncContrastListHandle()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.groupManage-page {
  position: relative;
  .icon-jia {
    font-size: 12px;
  }
}
</style>

